
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include <stdio.h>
#include <string.h>


typedef struct _Test {
    char msg[64];
    int a;
}Test;

int main(int argc, char *argv[])
{
    Test t;
    int fd = open("test.txt", O_WRONLY | O_CREAT, S_IRWXU);
    if (fd < 0) {
        fprintf(stderr, "open failed[1]\n");
        return -1;
    }
    t.a = 23;
    strncpy(t.msg, "Hello World", 12);
    write(fd, &t, sizeof(Test));
    close(fd);

    fd = open("test.txt", O_RDONLY);
    if (fd < 0) {
        fprintf(stderr, "open failed[2]\n");
        return -1;
    }
    memset(&t, 0, sizeof(Test));
    read(fd, &t, sizeof(Test));
    close(fd);

    printf("T.a(%d) T.msg(%s)\n", t.a, t.msg);

    return 0;
}
